package cn.edu.neusoft.wuguangsheng.flowerimpression.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

import cn.edu.neusoft.wuguangsheng.flowerimpression.R;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.RegistBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.SuccessBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.TListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.GetRegistRandModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.RegistModel;

public class RegistActivity extends BaseActivity implements View.OnClickListener{

    EditText regist_password;
    EditText regist_password1;
    EditText regist_phone;
    EditText regist_nickname;
    EditText regist_rand;
    Button regist_regist;
    RadioGroup rg;
    TextView regist_getrand;
    CheckBox regist_zctk;
    TextView regist_agreement;
    private Dialog mDialog;

    ImageView regist_seepassword,regist_seepassword1;

    ImageView regist_fh;
    String password,password1,phone,sex,nickname,rand;
    private int i = 60;
    boolean agreement_flag;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.arg1 == 0){
                regist_getrand.setText("点击重新获取");
                regist_getrand.setClickable(true);
                i=60;
            }
            else{
                regist_getrand.setText(msg.arg1+"秒后可重新发送");
                startTime();
                regist_getrand.setClickable(false);
        }
        }
    };
    @Override
    void initView() {
        setLayout_file(R.layout.activity_regist);
        regist_password = (EditText)findViewById(R.id.regist_password);
        regist_password1 = (EditText)findViewById(R.id.regist_password1);
        regist_phone = (EditText)findViewById(R.id.regist_phone);
        regist_nickname = (EditText)findViewById(R.id.regist_nickname);
        regist_regist = (Button)findViewById(R.id.regist_regist);
        rg = (RadioGroup)findViewById(R.id.regist_rg);
        regist_zctk = (CheckBox)findViewById(R.id.regist_zctk);
        regist_fh = (ImageView)findViewById(R.id.regist_fh);
        regist_getrand = (TextView)findViewById(R.id.regist_getrand);
        regist_agreement = (TextView)findViewById(R.id.regist_agreement);
        regist_rand = (EditText)findViewById(R.id.regist_rand);

        regist_seepassword = (ImageView)findViewById(R.id.regist_seepassword);
        regist_seepassword1 = (ImageView)findViewById(R.id.regist_seepassword1);

        regist_seepassword.setOnClickListener(this);
        regist_seepassword1.setOnClickListener(this);

        regist_fh.setOnClickListener(this);

        regist_regist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                phone = regist_phone.getText().toString().trim();
                password = regist_password.getText().toString().trim();
                password1 = regist_password1.getText().toString().trim();
                nickname = regist_nickname.getText().toString().trim();
                rand = regist_rand.getText().toString().trim();
                sex = rg.getCheckedRadioButtonId() == R.id.regist_sex1 ? "1" : "0";

                if (phone.equals("")||password.equals("")||password1.equals("")||nickname.equals("")||rand.equals("")){
                    Toast.makeText(RegistActivity.this,"输入信息不能有空值",Toast.LENGTH_SHORT).show();
                }
                else if(!regist_zctk.isChecked()){
                    Toast.makeText(RegistActivity.this,"同意条款方可注册",Toast.LENGTH_SHORT).show();
                }
                else if (!isMobileNO(phone)){
                    Toast.makeText(RegistActivity.this,"电话号码格式错误",Toast.LENGTH_SHORT).show();
                }
                else if (password.length()<6&&password1.length()<6){
                    Toast.makeText(RegistActivity.this,"密码为6-20位",Toast.LENGTH_SHORT).show();
                }
                else if (!password.equals(password1)){
                    Toast.makeText(RegistActivity.this,"两次密码输入不一致",Toast.LENGTH_SHORT).show();
                }
                else{
                    RegistModel registModel = new RegistModel();
                    registModel.regist(phone,password,sex,nickname,rand,registBeanTListener);
                }
            }
        });
    }

    @Override
    void initEvent() {
        regist_getrand.setOnClickListener(this);
        regist_agreement.setOnClickListener(this);


    }

    @Override
    void initDate() {

    }



    TListener<RegistBean> registBeanTListener = new TListener<RegistBean>() {

        @Override
        public void onResponse(RegistBean registBean) {
            String success = registBean.getSuccess();
            if (success.equals("1")){
               // Toast.makeText(RegistActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(RegistActivity.this,LoginActivity.class);
                intent.putExtra("phone",phone);
                setResult(RESULT_OK,intent);
                Toast.makeText(RegistActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                finish();
            }
            else if (success.equals("2")){
                Toast.makeText(RegistActivity.this,"该手机号已被注册",Toast.LENGTH_SHORT).show();
            }
            else if (success.equals("3")){
                Toast.makeText(RegistActivity.this,"验证码错误",Toast.LENGTH_SHORT).show();
            }
            else{
                Toast.makeText(RegistActivity.this,"注册失败",Toast.LENGTH_SHORT).show();
            }
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(RegistActivity.this,"错误",Toast.LENGTH_SHORT).show();
        }
    };

    TListener<SuccessBean> successBeanTListener = new TListener<SuccessBean>() {
        @Override
        public void onResponse(SuccessBean successBean) {
            String success = successBean.getSuccess();
            if (success.equals("1")){
                Toast.makeText(RegistActivity.this,"验证码已发送，请注意查收",Toast.LENGTH_SHORT).show();
                startTime();
                DialogThridUtils.closeDialog(mDialog);
            }
            else if (success.equals("2")){
                Toast.makeText(RegistActivity.this,"该号码已被注册",Toast.LENGTH_SHORT).show();
                DialogThridUtils.closeDialog(mDialog);
            }
            else if (success.equals("0")){
                Toast.makeText(RegistActivity.this,"注册失败",Toast.LENGTH_SHORT).show();
                DialogThridUtils.closeDialog(mDialog);
            }
            else{
                Toast.makeText(RegistActivity.this,"注册异常",Toast.LENGTH_SHORT).show();
                DialogThridUtils.closeDialog(mDialog);
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(RegistActivity.this,"网络异常",Toast.LENGTH_SHORT).show();
            DialogThridUtils.closeDialog(mDialog);
        }
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.regist_fh:
                finish();
                break;
            case R.id.regist_seepassword:
                setPasswordVisibility();
                break;
            case R.id.regist_seepassword1:
                setPasswordVisibility1();
                break;
            case R.id.regist_getrand:
                String phones = regist_phone.getText().toString().trim();
                if (phones.equals("")){
                    Toast.makeText(RegistActivity.this,"电话号码不能为空",Toast.LENGTH_SHORT).show();
                    break;
                }
                else if (!isMobileNO(phones)){
                    Toast.makeText(RegistActivity.this,"电话号码违规错误，请重新输入",Toast.LENGTH_SHORT).show();
                    break;
                }
                else {
                    mDialog = WeiboDialogUtils.createLoadingDialog(RegistActivity.this, "发送中...");
                    GetRegistRandModel getRegistRandModel = new GetRegistRandModel();
                    getRegistRandModel.getRegistRand(phones,successBeanTListener);

                    break;
                }

            case R.id.regist_agreement:
                Intent intent = new Intent(RegistActivity.this,AgreementActivity.class);
                startActivity(intent);
        }
    }

    private static boolean isMobileNO(String mobiles) {//判断是否为手机号的正则表达式
        String telRegex = "[1][35789]\\d{9}";
        if (TextUtils.isEmpty(mobiles)) return false;
        else return mobiles.matches(telRegex);
    }



    private void setPasswordVisibility(){
        if (regist_seepassword.isSelected()) {
            regist_seepassword.setSelected(false);
            //密码不可见
            regist_password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

        } else {
            regist_seepassword.setSelected(true);
            //密码可见
            regist_password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        }

    }

    private void setPasswordVisibility1(){
        if (regist_seepassword1.isSelected()) {
            regist_seepassword1.setSelected(false);
            //密码不可见
            regist_password1.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

        } else {
            regist_seepassword1.setSelected(true);
            //密码可见
            regist_password1.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        }

    }

    public void startTime(){
        Timer timer = new Timer();
        TimerTask task =  new TimerTask() {
            @Override
            public void run() {
                if(i>0){
                    i--;
                    Message message = handler.obtainMessage();
                    message.arg1 = i;
                    handler.sendMessage(message);
                }
            }
        };
        timer.schedule(task,1000);
    }

}
